package com.blb.community.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.community.entity.SysArea;
import com.blb.community.entity.ZyCommunity;
import com.blb.community.entity.vo.CommunityAreaVO;
import com.blb.community.entity.vo.CommunityVO;
import com.blb.community.mapper.SysAreaMapper;
import com.blb.community.mapper.ZyCommunityMapper;
import com.blb.community.service.IZyCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 小区  服务实现类
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Service
public class ZyCommunityServiceImpl extends ServiceImpl<ZyCommunityMapper, ZyCommunity> implements IZyCommunityService {

    @Autowired
    private ZyCommunityMapper communityMapper;
    @Autowired
    private SysAreaMapper areaMapper;

    @Override
    public IPage<CommunityVO> getCommunity(CommunityVO communityVO, IPage<CommunityVO> page) {

        return communityMapper.getCommunity(communityVO, page);
    }

    @Override
    public IPage<CommunityAreaVO> getCommunityArea(CommunityAreaVO communityAreaVO, IPage<CommunityAreaVO> page) {
        return communityMapper.getCommunityArea(communityAreaVO,page);
    }

    @Transactional
    @Override
    public void addCommunity(CommunityAreaVO communityAreaVO) {

        Integer id = communityAreaVO.getId();

        SysArea area1 = areaMapper.getById(id);
        Integer code = area1.getCode();
        String qucode = code.toString();

        Integer parentId = area1.getParentId();
        SysArea cityByid = areaMapper.getCityByid(parentId);
        Integer code1 = cityByid.getCode();
        String citycode = code1.toString();

        Integer parentId1 = cityByid.getParentId();
        SysArea sysArea1 = areaMapper.getshenByid(parentId1);
        Integer code2 = sysArea1.getCode();
        String shencode = code2.toString();

        communityAreaVO.setCommunityProvenceCode(shencode);
        communityAreaVO.setCommunityCityCode(citycode);
        communityAreaVO.setCommunityTownCode(qucode);

        communityMapper.insert(communityAreaVO);
    }

    @Transactional
    @Override
    public void updateCommunity(CommunityAreaVO communityAreaVO) {
        Integer id = communityAreaVO.getId();

        SysArea area1 = areaMapper.getById(id);
        Integer code = area1.getCode();
        String qucode = code.toString();

        Integer parentId = area1.getParentId();
        SysArea cityByid = areaMapper.getCityByid(parentId);
        Integer code1 = cityByid.getCode();
        String citycode = code1.toString();

        Integer parentId1 = cityByid.getParentId();
        SysArea sysArea1 = areaMapper.getshenByid(parentId1);
        Integer code2 = sysArea1.getCode();
        String shencode = code2.toString();

        communityAreaVO.setCommunityProvenceCode(shencode);
        communityAreaVO.setCommunityCityCode(citycode);
        communityAreaVO.setCommunityTownCode(qucode);

        communityMapper.updateById(communityAreaVO);
    }

    @Override
    public void updateDeptId(Long communityId, Long deptId) {
        communityMapper.updateDeptId(communityId,deptId);
    }


}
